package com.clsfd.charging;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Properties;

import javax.net.ssl.SSLEngineResult.Status;

import com.clsfd.database.DatabaseConnection;

public class ChargeCustomer {

	DatabaseConnection DBConn=null;
	public ChargeCustomer(DatabaseConnection dbconnection){
		DBConn=dbconnection;
	}
	
	public  ChargeStatus Charge(String msisdn, ChargeEvent chrgevent, String description) throws IOException{
		if ((msisdn.startsWith("9472") ||msisdn.startsWith("9473"))){
			if(chrgevent.equals(ChargeEvent.SUSCRIBE)){				
				return ChargeEtisalat(msisdn,chrgevent, "WAP10_V-Alerts",description);				
			}
			else if(chrgevent.equals(ChargeEvent.PUBLISHADVERT)){					
				return ChargeEtisalat(msisdn,chrgevent, "ADVERT",description);				
			}		
		}
		else if ( (msisdn.startsWith("9477") ||msisdn.startsWith("9476"))){
			if(chrgevent.equals(ChargeEvent.SUSCRIBE)){				
				return ChargeEtisalat(msisdn,chrgevent, "SUBSCRIBEALERT",description);				
			}
			else if(chrgevent.equals(ChargeEvent.PUBLISHADVERT)){					
				return ChargeEtisalat(msisdn,chrgevent, "ADVERT",description);				
			}		
		}
		
		return ChargeStatus.TEMPERROR;
		
	}
	
	private ChargeStatus ChargeEtisalat(String msisdn,ChargeEvent chrgevent, String serviceCode, String description) {
		String response="";
		try{
			URI charginguri=  new URI("http", null, "10.1.80.21", 8080, "/cgi-bin/erl/omniecg_web/chrg_sub","ContentProvider=APALYA&MSISDN="+msisdn+"&SERVICECODE="+serviceCode+"&Description=Optional", null);
			URL chargingurl = charginguri.toURL();
		    BufferedReader in = new BufferedReader(new InputStreamReader(chargingurl.openStream()));
			response=in.readLine();
		    in.close();
		}catch (Exception e){
			
		}
	    printCDR(msisdn,chrgevent,serviceCode,description,response);
	    if (response.equals("1")) return ChargeStatus.SUCCESS;
	    else if (response.equals("0")) return ChargeStatus.INSUFFICIENTBAL;
	    else return ChargeStatus.TEMPERROR;	
	    		
	    		
	}

	private void printCDR(String msisdn, ChargeEvent chrgevent,String serviceCode, String description, String Status){
		
	}



}
